package com.frxuexi.mine.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.mine.entity.LeaveEntity
import com.frxuexi.mine.intent.UserIntent
import com.frxuexi.mine.viewmodel.UserViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

@Composable
fun LeaveListPage(navHostController: NavHostController,vm:UserViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<LeaveEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        var list2=it.data as Collection<LeaveEntity>
                        list.addAll(list2.filter {
                            !it.startTime.isNullOrEmpty() && !it.endTime.isNullOrEmpty() && !it.type.isNullOrEmpty()
                        })
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(UserIntent.getLeaves("1"))
    })

    Column (
        Modifier
            .fillMaxSize()
            .padding(20.dp)){

        BaseTitle(navHostController = navHostController, title = "请假记录", btn = "申请"){
            navHostController.navigate(RoutePath.ASKLEAVE)
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 15.dp),
            horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                LeaveItem(entity = it,navHostController)
            }
        })
    }
}

@Composable
fun LeaveItem(entity: LeaveEntity,navHostController: NavHostController){
    Row(
        Modifier
            .fillMaxWidth()
            .background(colorResource(id = R.color.lightpurple), RoundedCornerShape(10.dp))
            .padding(vertical = 15.dp)
            .clickable {
                       val json = Gson().toJson(entity)
                navHostController.navigate(RoutePath.LEAVE_DETAIL+"/$json")
            },
        horizontalArrangement = Arrangement.SpaceAround, verticalAlignment = Alignment.CenterVertically) {
        Text(text = entity.type)
        Text(text = entity.startTime.substring(0,10))
        Text(text = entity.endTime.substring(0,10))
        var color = if(entity.status=="通过") Color.Green else if(entity.status=="拒绝") Color.Red else Color.LightGray
        Text(text = entity.status,Modifier.background(color, RoundedCornerShape(15.dp)).padding(10.dp,5.dp))
    }
}